//
//  TTDiscoverMainViewController.m
//  Adorable_Things
//
//  Created by huzhiyan on 16/1/11.
//  Copyright © 2016年 Banana Inc. All rights reserved.
//

#import "TTDiscoverMainViewController.h"
#import "TTDiscoverAlbumViewController.h"
#import "TTDiscoverArticleViewController.h"
#import "ImageLabelView.h"
#import "MLKMenuPopover.h"
#define kArticleUrl @"http://api.menghuoapp.com/v1/post/list"
#define MENU_POPOVER_FRAME  CGRectMake(kWidth * 2 / 3 - 20, kWidth / 5, kWidth / 2 - 20, kHeight / 3)

@interface TTDiscoverMainViewController () <UIGestureRecognizerDelegate, MLKMenuPopoverDelegate, UIScrollViewDelegate>

@property (nonatomic, strong) TTDiscoverAlbumViewController *albumVC;
@property (nonatomic, strong) TTDiscoverArticleViewController *articleVC;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) ImageLabelView *articleView;
@property (nonatomic, strong) ImageLabelView *albumView;
@property (nonatomic, strong) UIButton *rightButton;
@property (nonatomic, strong) UIButton *topButton;
@property(nonatomic,strong) MLKMenuPopover *menuPopover;
@property(nonatomic,strong) NSArray *menuItems;

@end

@implementation TTDiscoverMainViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBarHidden = YES;
    
    [self addOwnNav];
    [self addChildVCTitle];
    [self addChildVC];
    [self changeChildVC];
    [self returnToTop];
    
    self.menuItems = [NSArray arrayWithObjects:@"按更新时间降序", @"按更新时间升序", @"按点赞数降序", @"按点赞数升序", nil];
}

// 自定义导航栏
- (void)addOwnNav
{
    self.navView = [[TTNavView alloc] initWithFrame:CGRectMake(0, 20, kWidth, 44)];
    self.navView.titleLabel.text = @"萌物志";
    [self.view addSubview:self.navView];
    
    self.rightButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.rightButton.frame = CGRectMake(kWidth - 35, 15, 20, 20);
    [self.rightButton setImage:[UIImage imageNamed:@"iconfont-iconfonticonloudoutu"] forState:(UIControlStateNormal)];
    [self.rightButton setTintColor:[UIColor grayColor]];
    [self.rightButton addTarget:self action:@selector(sortButtonClick:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.navView addSubview:self.rightButton];
    
    UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(kWidth / 2 - 1, 75, 2, 25)];
    line.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:line];

}

- (void)sortButtonClick:(UIButton *)button
{
    [self.menuPopover dismissMenuPopover];
    
    self.menuPopover = [[MLKMenuPopover alloc] initWithFrame:MENU_POPOVER_FRAME menuItems:self.menuItems];
    
    self.menuPopover.layer.masksToBounds = YES;
    self.menuPopover.layer.cornerRadius = 10;
    
    self.menuPopover.menuPopoverDelegate = self;
    [self.menuPopover showInView:self.view];

}


#pragma mark MLKMenuPopoverDelegate
// 实现代理方法  下拉菜单的点击
- (void)menuPopover:(MLKMenuPopover *)menuPopover didSelectMenuItemAtIndex:(NSInteger)selectedIndex
{
    [self.menuPopover dismissMenuPopover];
    switch (selectedIndex) {
        case 0:
            _articleVC.order_dir = @"1";
            _articleVC.order_by = @"1";
            break;
            
        case 1:
            _articleVC.order_dir = @"1";
            _articleVC.order_by = @"0";
            break;
            
        case 2:
            _articleVC.order_dir = @"2";
            _articleVC.order_by = @"1";
            break;
            
        case 3:
            _articleVC.order_dir = @"2";
            _articleVC.order_by = @"0";
            break;
            
        default:
            break;
    }
    [_articleVC setUpData];
}



// 添加子控制器标题
- (void)addChildVCTitle
{
    self.articleView = [[ImageLabelView alloc] initWithFrame:CGRectMake(0, 70, kWidth / 2, 40)];
    _articleView.imageV.image = [UIImage imageNamed:@"iconfont-zhanshiwenzhang-2"];
    _articleView.label.text = @"文章";
    _articleView.label.font = [UIFont systemFontOfSize:14];
    _articleView.label.textColor = [UIColor redColor];
    [self.view addSubview:_articleView];
    
    self.albumView = [[ImageLabelView alloc] initWithFrame:CGRectMake(_articleView.right, _articleView.top, kWidth / 2, 40)];
    _albumView.imageV.image = [UIImage imageNamed:@"iconfont-zhuanji"];
    _albumView.label.text = @"专辑";
    _albumView.label.font = [UIFont systemFontOfSize:14];
    _albumView.label.textColor = [UIColor lightGrayColor];
    [self.view addSubview:_albumView];

}

// 添加子控制器
- (void)addChildVC
{
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 104, kWidth, kHeight - 153)];
    self.scrollView.contentSize = CGSizeMake(2 * kWidth, self.scrollView.height);
    self.scrollView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"LaunchImage-568h@2x"]];
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounces = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.delegate = self;
    [self.view addSubview:self.scrollView];
    
    self.articleVC = [[TTDiscoverArticleViewController alloc] init];
    _articleVC.view.frame = CGRectMake(0, 0, kWidth, self.scrollView.height + 49);
    [self.scrollView addSubview:_articleVC.view];
    [self addChildViewController:_articleVC];
    
    self.albumVC = [[TTDiscoverAlbumViewController alloc] init];
    _albumVC.view.frame = CGRectMake(kWidth, 0, kWidth, self.scrollView.height);
    [self.scrollView addSubview:_albumVC.view];
    [self addChildViewController:_albumVC];
}

// 切换子控制器
- (void)changeChildVC
{
    UITapGestureRecognizer *articleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(articleTap)];
    [_articleView addGestureRecognizer:articleTap];
    articleTap.delegate = self;
    articleTap.cancelsTouchesInView = NO;
    
    UITapGestureRecognizer *albumTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(albumTap)];
    [_albumView addGestureRecognizer:albumTap];
    albumTap.delegate = self;
    albumTap.cancelsTouchesInView = NO;
}

// 回到顶部按钮
- (void)returnToTop
{
    self.topButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.topButton.frame = CGRectMake(kWidth * 5 / 6, kHeight * 3 / 4 - 20, kWidth / 9, kWidth / 9);
    [self.topButton setImage:[UIImage imageNamed:@"iconfont-launch"] forState:(UIControlStateNormal)];
    self.topButton.layer.masksToBounds = YES;
    self.topButton.layer.cornerRadius = kWidth / 18;
    self.topButton.alpha = .95;
    [self.topButton addTarget:self action:@selector(topButtonClick) forControlEvents:(UIControlEventTouchUpInside)];
    self.topButton.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.topButton];
    [self.view bringSubviewToFront:self.topButton];
}

// 回到顶部点击方法
- (void)topButtonClick
{
    if (_scrollView.contentOffset.x == 0) {
        [_articleVC.tableView setContentOffset:CGPointMake(_articleVC.tableView.contentOffset.x, 0) animated:YES];
    } else {
        [_albumVC.collectionView setContentOffset:CGPointMake(_albumVC.collectionView.contentOffset.x, 0) animated:YES];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (_scrollView.contentOffset.x == kWidth) {
        _articleView.imageV.image = [UIImage imageNamed:@"iconfont-zhanshiwenzhang"];
        _articleView.label.textColor = [UIColor lightGrayColor];
        _albumView.imageV.image = [UIImage imageNamed:@"iconfont-zhuanji-2"];
        _albumView.label.textColor = [UIColor redColor];
        self.rightButton.hidden = YES;
    } else if(_scrollView.contentOffset.x == 0) {
        _albumView.imageV.image = [UIImage imageNamed:@"iconfont-zhuanji"];
        _albumView.label.textColor = [UIColor lightGrayColor];
        _articleView.imageV.image = [UIImage imageNamed:@"iconfont-zhanshiwenzhang-2"];
        _articleView.label.textColor = [UIColor redColor];
        self.rightButton.hidden = NO;
    }
 
    
    
    
}

// 点击文章标题切换视图
- (void)articleTap
{
    [UIView animateWithDuration:0.5 animations:^{
        _albumView.imageV.image = [UIImage imageNamed:@"iconfont-zhuanji"];
        _albumView.label.textColor = [UIColor lightGrayColor];
        _articleView.imageV.image = [UIImage imageNamed:@"iconfont-zhanshiwenzhang-2"];
        _articleView.label.textColor = [UIColor redColor];
        [_scrollView setContentOffset:CGPointMake(0, 0) animated:NO];
        self.rightButton.hidden = NO;
    }];
}

// 点击专辑标题切换视图
- (void)albumTap
{
    [UIView animateWithDuration:0.5 animations:^{
        _articleView.imageV.image = [UIImage imageNamed:@"iconfont-zhanshiwenzhang"];
        _articleView.label.textColor = [UIColor lightGrayColor];
        _albumView.imageV.image = [UIImage imageNamed:@"iconfont-zhuanji-2"];
        _albumView.label.textColor = [UIColor redColor];
        [_scrollView setContentOffset:CGPointMake(kWidth, 0) animated:NO];
        self.rightButton.hidden = YES;
    }];
}

// 手势代理方法
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
